#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/7/11 18:39
# @File    : login.py
# @Description : -i http://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn


import tkinter as tk
import ttkbootstrap as ttk
from tkinter import PhotoImage
from ttkbootstrap.constants import *
from PIL import Image, ImageTk, ImageGrab
from ttkbootstrap.dialogs import Messagebox


class DataEntryForm(ttk.Frame):

    def __init__(self, master):
        super().__init__(master, padding=(10, 5))
        self.pack(fill=BOTH, expand=YES)

        # form variables
        self.name = ttk.StringVar(value="")
        self.address = ttk.StringVar(value="")
        self.phone = ttk.StringVar(value="")

        self.images = [
            PhotoImage(name='img', file='./image/img.png'),
            PhotoImage(name='submit', file='./image/icons8_play_24px.png'),
            PhotoImage(name='buy', file='./image/icons8_desktop_40px.png'),
            PhotoImage(name='reset', file='./image/icons8_settings_24px.png')
        ]

        col1 = ttk.Frame(self, padding=10)
        col1.grid(row=0, column=0, sticky=NSEW)
        dev_info = ttk.Labelframe(col1, text='login info', padding=10)
        dev_info.pack(side=TOP, fill=BOTH, expand=YES)

        # header
        dev_info_header = ttk.Frame(dev_info, padding=5)
        dev_info_header.pack(fill=X)

        # image
        ttk.Label(dev_info, image='img').pack(fill=X)

        # img = tk.PhotoImage(file='pyflow.png')
        # b_lable = ttk.Label(bottom, image=img)
        # b_lable.image = img
        # b_lable.pack(side='top')

        dev_info_body = ttk.Frame(dev_info, padding=5)
        dev_info_body.pack(fill=X)

        # form entries
        self.create_form_entry(dev_info_body, "用户:", self.name)
        self.create_form_entry(dev_info_body, "密码:", self.address)
        self.create_buttonbox(dev_info_body)

        # Column 2
        col2 = ttk.Frame(self, padding=10)
        col2.grid(row=0, column=1, sticky=NSEW)

        # scrolling
        scrolling = ttk.Labelframe(col2, text='Scrolling', padding=(15, 10))
        scrolling.pack(side=TOP, fill=BOTH, expand=YES)

        op1 = ttk.Checkbutton(scrolling, text='Scrolling', variable='op1')
        op1.pack(fill=X, pady=5)




    def create_form_entry(self, master, label, variable):

        """Create a single form entry"""
        container = ttk.Frame(master)
        container.pack(fill=X, expand=YES, pady=5)

        lbl = ttk.Label(master=container, text=label.title(), width=10)
        lbl.pack(side=LEFT, padx=5)

        ent = ttk.Entry(master=container, textvariable=variable)
        ent.pack(side=LEFT, padx=5, fill=X, expand=YES)

    def create_buttonbox(self, master):
        """Create the application buttonbox"""
        container = ttk.Frame(master)
        container.pack(fill=X, expand=YES, pady=(15, 10))
        # 分割线
        ttk.Separator(container).pack(padx=5, pady=5, fill=tk.X)

        sub_btn = ttk.Button(
            master=container,
            text="登录",
            command=self.on_submit,
            bootstyle=SUCCESS,
            width=6,
        )
        sub_btn.pack(side=RIGHT, padx=5)
        sub_btn.focus_set()

        cnl_btn = ttk.Button(
            master=container,
            text="退出",
            command=self.on_cancel,
            bootstyle=DANGER,
            width=6,
        )
        cnl_btn.pack(side=RIGHT, padx=5)

        btn = ttk.Button(
            master=container,
            image='reset',
            text='清除',
            bootstyle=LINK,
            compound=LEFT,
            command=self.callback
        )
        btn.pack(side=RIGHT, padx=5)

        buy_now = ttk.Button(
            master=container,
            image='buy',
            compound=RIGHT,
            command=self.callback
        )
        buy_now.pack(padx=5, fill=X)

    def on_submit(self):
        """Print the contents to console and return the values."""
        print("Name:", self.name.get())
        print("Address:", self.address.get())
        print("Phone:", self.phone.get())
        Messagebox.ok(
            title='Button callback',
            message="OK"
        )
        return self.name.get(), self.address.get(), self.phone.get()

    def on_cancel(self):
        """Cancel and close the application."""
        self.quit()

    def callback(self):
        """Demo callback"""
        Messagebox.ok(
            title='Button callback',
            message="You pressed a button."
        )


if __name__ == "__main__":
    app = ttk.Window("Data Entry", "flatly", resizable=(False, False))
    login = DataEntryForm(app)
    print(login)
    app.mainloop()